﻿using System;
using Serilog;
using System.Security.Claims;
using MiniX.Models;
using Microsoft.EntityFrameworkCore;
using MiniX.API.Data;
using Microsoft.CodeAnalysis;

namespace MiniX.API.Filters
{
    /// <summary>
    /// 封装操作日志类
    /// </summary>
	public class LogFilter
	{
        /// <summary>
        /// 构造方法
        /// </summary>
        public LogFilter()
		{

        }

        /// <summary>
        /// 添加日志
        /// </summary>
        /// <param name="_context">数据库连接上下文</param>
        /// <param name="log">日志对象</param>
        /// <param name="type_id">日志类型</param>
        /// <param name="member_id">会员ID</param>
        /// <param name="log_code">日志代码</param>
        /// <param name="log_content">日志内容</param>
        /// <param name="client_ip">客户端IP</param>
        /// <param name="log_remark">备注</param>
        public static void AddLogs(MiniXApiContext _context, Serilog.Core.Logger log, int type_id, long member_id, string log_code, string log_content, string client_ip, string log_remark)
        {
            try
            {
                _context.mini_logs.Add(new Logs()
                {
                    type_id = type_id,
                    user_id = -1,
                    member_id = -1,
                    log_code = log_code,
                    platform_id = 1,
                    log_content = log_content,
                    client_ip = client_ip,
                    page_url = "",
                    referrer_url = "",
                    user_agent = "",
                    user_cookies = "",
                    log_remark = log_remark,
                    created_at = DateTime.Now
                });
                _context.SaveChanges();
            }
            catch (Exception ex)
            {
                log.Fatal(ex, " -> 添加操作日志");
            }
        }
    }
}

